package com.dries.security.core.social.wechat.config;

import com.dries.security.core.properties.SecurityCoreProperties;
import com.dries.security.core.properties.social.WeChatProperties;
import com.dries.security.core.social.config.SocialAutoConfigurerAdapter;
import com.dries.security.core.social.view.DriesConnectView;
import com.dries.security.core.social.wechat.connect.WeChatConnectionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.web.servlet.View;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/9/17 11:43
 */
@Configuration
@ConditionalOnProperty(prefix = "dries.security.social.weixin", name = "appId")
public class WeChatAutoConfig extends SocialAutoConfigurerAdapter {

    @Resource
    private SecurityCoreProperties coreProperties;

    @Override
    protected ConnectionFactory<?> createConnectionFactory() {

        WeChatProperties weixin = coreProperties.getSocial().getWeixin();

        return new WeChatConnectionFactory(weixin.getProviderId(), weixin.getAppId(),
                weixin.getAppSecret());
    }


    @Bean({"/connect/weixinConnect","/connect/weixinConnected"})
    @ConditionalOnMissingBean(name = "wechatConnectView")
    public View wechatConnectView(){
        return new DriesConnectView();
    }

}
